<?php
/**
 * Created by xuyajun.
 * User: xuyajun
 * Date: 2019/10/30
 * Time: 15:55
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Support\Facades\Request;
use DB;
use \Illuminate\Support\Facades\Input;
use App\ModelStatistic\StatisticCommon;

class AccountInfo extends Model
{
    public static function attachAccountCreateTime(&$list, $account_field, $create_time_field)
    {
        if (empty($list) ) {
            return $list;
        }

        $account_field = $account_field ?: 'account';
        $ids = [];
        foreach ($list as $obj) {
            $ids[] = $obj->{$account_field};
        }

        $account_list = DB::connection('mysql_bgac')->table('account_info')->select('account', 'createTime', 'ip')->whereIn('account', $ids)->get();
        $account_list_map = [];
        foreach($account_list as $obj) {
            $account_list_map[$obj->account] = [
                'create_time' => $obj->createTime,
                'ip' => $obj->ip,
            ];
        }
        // \Log::debug(__METHOD__, ['account create time list', $account_list_map]);
        foreach($list as &$obj) {
            $obj->{$create_time_field} = $account_list_map[$obj->{$account_field}]['create_time'];
            $obj->ip = $account_list_map[$obj->{$account_field}]['ip'];
        }
    }

    public static function deleteAccount($account = null) {
        if (!$account) {
            return 0;
        }
        DB::connection('mysql_bgac')->table('account_agent')->where('account', $account)->delete();
        return 0;
    }

    public static function encloseAccount($account = null, $delete_at = 2) {
        if (!$account) {
            return 0;
        }
        DB::connection('mysql_bgac')->table('account_agent')->where('account', $account)->update(['deleted_at'=> $delete_at]);
        return 0;
    }
}